package com.jwell.panprovider.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panprovider.contract.TransportPlaneInfoContract
import com.jwell.panprovider.entity.AppFuelPlanDtlDto
import com.jwell.panprovider.model.TransportPlaneInfoModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/24.
 * 供应详情-计划信息
 */
class TransportPlaneInfoPresenter : TransportPlaneInfoContract.Presenter, BasePresenter<TransportPlaneInfoContract.View>() {

    private val model: TransportPlaneInfoModel by lazy {
        TransportPlaneInfoModel()
    }

    override fun appUpdateEstimate(fuelPlanId: Int, estimateCarCount: Int) {
        getView()?.showProgress("", true)
        model.appUpdateEstimate(fuelPlanId, estimateCarCount, object : BaseSubscriber(getView()) {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getData(fuelPlanId)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }

    override fun getData(id: Int) {
        model.getData(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val data = Gson().fromJson<AppFuelPlanDtlDto>(data.getString("result"), object : TypeToken<AppFuelPlanDtlDto>() {
                }.type)
                getView()?.showData(data)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
            }
        })
    }
}